package com.example.bsxk.service.impl;

import com.example.bsxk.dao.KZTeacherAllThesisDao;
import com.example.bsxk.dto.KZTeacherAllThesis;
import com.example.bsxk.entity.StudentThesis;
import com.example.bsxk.entity.StudentThesisExample;
import com.example.bsxk.entity.Thesis;
import com.example.bsxk.entity.ThesisExample;
import com.example.bsxk.mapper.StudentThesisMapper;
import com.example.bsxk.mapper.ThesisMapper;
import com.example.bsxk.service.KZTeacherChoiceStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class KZTeacherChoiceStudentServerImpl implements KZTeacherChoiceStudentService {
    @Autowired
    KZTeacherAllThesisDao teacherAllThesisDao;
    @Autowired
    StudentThesisMapper studentThesisMapper;
    @Autowired
    ThesisMapper thesisMapper;
    @Override
    public List<KZTeacherAllThesis> selectAll(Long id,Integer year) {
        return teacherAllThesisDao.teacherAll(id,year);
    }

    @Override
    public List<KZTeacherAllThesis> teacherAllByYear(Integer year) {
        return teacherAllThesisDao.teacherAllByYear(year);
    }

    @Override
    public Boolean istrue(Integer tid, Long sid) {

//        studentThesisExample.createCriteria().andTidEqualTo(tid).andUidEqualTo(sid);
//        //在课程-用户表中查询该课题有没有该学生，没有返false
//        List<StudentThesis> studentThesis =  new ArrayList<>();
//        studentThesis = studentThesisMapper.selectByExample(studentThesisExample);
//        StudentThesis studentThesis1 = new StudentThesis();
//         int flog = 0;
//        for (int i = 0 ; i < studentThesis.size();i++){
//            studentThesis1 = studentThesis.get(i);
//            if (studentThesis1.getTid()==tid&&studentThesis1.getUid()==sid){
//                flog=1;
//                break;
//            }
//        }
//        if (flog == 0){
//            return false;
//        }
        StudentThesisExample studentThesisExample = new StudentThesisExample();
        long uid =sid;
        if (studentThesisMapper.selectByPrimaryKey(uid,tid)==null){
            return false;
        }

        //课题确定该学生，删除本课题其他学生
        studentThesisExample.createCriteria().andTidEqualTo(tid).andUidNotEqualTo(sid);
        studentThesisMapper.deleteByExample(studentThesisExample);

        //删除该学生在其他课题里的记录
        studentThesisExample.createCriteria().andUidEqualTo(sid).andTidNotEqualTo(tid);
        studentThesisMapper.deleteByExample(studentThesisExample);

        //设置该课题状态为选定状态
        Thesis thesis = thesisMapper.selectByPrimaryKey(tid);
        thesis.setStatus("3");
        thesisMapper.updateByPrimaryKey(thesis);
        return true;
    }
}
